<template>
  <div class="app">
    <div class="box">
      <el-form ref="ruleForm" class="buttons">
        <el-form-item label="学科名称">
          <el-input v-model="subjectName"></el-input>
        </el-form-item>
        <el-button size="small" @click="resetForm(subjectName)" class="small"
          >清除</el-button
        >
        <el-button size="small" class="smalls" type="primary" @click="search"
          >搜索</el-button
        >
      </el-form>
      <el-button type="success" class="add el-icon-edit" @click="add"
        >新增学科</el-button
      >

      <div class="prompt el-icon-info">
        <span>数据一共{{ and }}条</span>
      </div>
      <!-- 表格-->
      <template>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column
            type="index"
            highlight-current-row
            label="序号"
            width="50"
          >
          </el-table-column>
          <el-table-column prop="subjectName" label="学科名称" width="145px">
          </el-table-column>
          <el-table-column prop="username" label="创建者" width="145px">
          </el-table-column>
          <el-table-column prop="addDate" label="创建日期" width="145px">
            <template slot-scope="scope">
              {{ dayFormat(scope.row.addDate) }}
            </template>
          </el-table-column>
          <el-table-column label="前台是否显示" width="145">
            <template v-slot="scope">
              {{ formatEmployment(parseInt(scope.row.isFrontDisplay)) }}
            </template>
          </el-table-column>
          <el-table-column
            prop="twoLevelDirectory"
            label="二级目录"
            width="145px"
          >
          </el-table-column>
          <el-table-column prop="tags" label="标签" width="140px">
          </el-table-column>
          <el-table-column prop="totals" label="题目数量" width="140px">
          </el-table-column>
          <el-table-column prop="name" label="操作">
            <template v-slot="scope">
              <el-button
                type="text"
                size="small"
                @click="classification(scope.row)"
                >学科分类</el-button
              >
              <el-button type="text" size="small" @click="tiao(scope.row)"
                >学科标签</el-button
              >
              <el-button
                type="text"
                size="small"
                @click="Modifythe(scope.row.id, scope.row)"
                >修改</el-button
              >
              <el-button type="text" size="small" @click="odd(scope.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- 添加弹框 -->
      <subjects
      @abb="getlist()"
        @change="ass"
        :tableData="tableData"
        @acc="getlist()"
        ref="from"
      ></subjects>
      <!-- 分页 -->

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :page-size="pagTags.pagesize"
        layout="prev, pager, next, sizes,jumper"
        :total="and"
        :page-sizes="[10, 20, 30, 40]"
      >
      </el-pagination>

      <!-- <el-pagination background layout="prev, pager, next" :total="1000">
      </el-pagination> -->
    </div>
  </div>
</template>

<script>
import { dateinit } from '@/utils/index.js'
import { list, remove } from '@/api/hmmm/subjects'
import { hireType } from '@/api/hmmm/constants'
import subjects from '@/module-hmmm/components/subjects-add.vue'

export default {
  name: 'heads',
  components: {
    subjects
  },
  data () {
    return {
      subjectName: '',
      and: 0,
      tableData: [],
      pagTags: {
        page: 1,
        pagesize: 10
      },
      value: 0,
      row: {},
      isall: false
    }
  },

  mounted () {},
  created () {
    this.getlist()
  },
  methods: {
    ass () {
      console.log('我是你八佰伴咩咩咩咩咩咩咩咩咩咩咩咩咩咩')
    },
    // 格式化时间
    dayFormat (time) {
      return dateinit(time)
    },
    // 获取数据列表
    async getlist () {
      console.log()
      const { data: res } = await list(this.pagTags)
      console.log(res)
      this.tableData = res.items
      this.and = res.counts
    },
    // 清空
    resetForm (subjectName) {
      console.log(subjectName)
      this.subjectName = ''
    },
    // 前台是否显示
    formatEmployment (num) {
      // console.log(num)
      // console.log(hireType)
      var obj = hireType.find((item) => item.id === num)
      // console.log(obj)
      return obj.value
    },
    // 搜索
    async search () {
      const res = await list({ subjectName: this.subjectName })
      this.tableData = res.data.items
      this.and = res.data.counts
      console.log(res, 9999999)
    },
    // 新增学科
    add () {
      this.isall = false
      console.log((this.$refs.from.dialogVisible = true))
      this.$refs.from.froms.subjectName = ''
      this.$refs.from.value = true
    },
    // 页面数量变化的时候触发
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      console.log(val)
      this.pagTags.pagesize = val
      this.getlist()
    },
    // 当前页改变的时候触发
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.pagTags.page = val
      this.getlist()
    },
    // 删除
    odd (id) {
      console.log(id)
      this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await remove({ id })
          if (res.status === 200) {
            this.getlist()
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 修改
    Modifythe (id, row) {
      this.$refs.from.froms.id = row.id
      this.$refs.from.dialogVisible = true
      console.log(row.subjectName)
      this.$refs.from.froms.subjectName = row.subjectName
      if (row.isFrontDisplay === 1) {
        this.$refs.from.value = true
      } else {
        this.$refs.from.value = false
      }
      // this.row = row
      this.$refs.from.isall = true
    },
    // 学科标签
    tiao (row) {
      this.$router.push({
        path: 'tags',
        query: {
          id: row.id,
          name: row.subjectName
        }
      })
    },
    // 学科分类
    classification (row) {
      this.$router.push({
        path: 'directorys',
        query: {
          id: row.id,
          name: row.subjectName
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.box {
  position: relative;
  .el-input--medium {
    width: 190px;
    ::v-deep.el-input__inner {
      height: 32px !important;
    }
  }
  .small {
    position: absolute;
    top: 0px;
    left: 265px;
  }
  .smalls {
    position: absolute;
    top: 0px;
    left: 325px;
  }
  ::v-deep.add {
    text-align: center;
    color: #ffff;
    background-color: #67c23a;
    width: 97px;
    height: 32px;
    position: absolute;
    top: 0px;
    right: 10px;
    padding: 7px 8px;
    font-size: 12px;
  }
  .prompt {
    width: 1297px;
    height: 34.8px;
    background-color: #f4f4f5;
    border-radius: 5px;
    // line-height: 34.8px;
    color: #909399;
    padding: 8px 16px;
    margin-bottom: 15px;
    span {
      margin-left: 5px;
      font-size: 12px;
    }
  }
  .el-button--text {
    font-size: 14px;
  }
  .el-pagination {
    float: right;
  }
}
</style>
